class WikiController < ApplicationController
  
  before_filter :set_history, :only => [ :edit, :index, :new, :view ]
  before_filter :authorized, :except => [ :index, :view ]
  
  # Non-AJAX Stuff
  def edit
    @page = Page.find(params[:id])
    unless request.get?
      @page.attributes = params[:page]
      if params[:commit] == 'Save'
        if @page.save
          redirect_to :action => :view, :id => @page
        else
          flash[:edit_page_error] = @page.errors.full_messages.join "<br />"
        end
      else
        @preview = @page.format_text
      end
    end
  end
  
  def index
    redirect_to :action => :view, :id => 'Index'
  end
  
  def new
    
  end
  
  def view
    @page = Page.find_by_title(params[:id])
  end
  
  # Remote Functions
  def remote_edit_preview
    @page = Page.find(params[:id])
    @page.attributes = params[:page]
    @preview = @page.format_text
  end
  
  def remote_new_preview
    @page = Page.new(params[:page])
    @preview = @page.format_text
  end
  
  def remote_save_edit
    @page = Page.find(params[:id])
    @saved = @page.update_attributes(params[:page])
  end
  
  def remote_save_new
    @page = Page.new(params[:page])
    @saved = @page.save
  end
  
  def remote_show_edit
    @page = Page.find(params[:id])
  end
  
  def remote_show_new
  end
  
end
